<?php
/* @var $this \yii\web\View */
/* @var $model */
/* @var $roleList */
?>
<div class="row">
    <div class="box">
        <div class="box-body">
            <?php
            $form = \yii\bootstrap\ActiveForm::begin();
            echo $form->field($model, 'user_name');
            echo $form->field($model, 'password')->passwordInput();
            echo $form->field($model, 'role')->dropDownList($roleList);
            echo $form->field($model, 'level')->dropDownList(Yii::$app->user->identity->getSubLevelList());
            echo $form->field($model, 'province', ['options' => ['data-level' => 4]])->dropDownList(['']);
            echo $form->field($model, 'city', ['options' => ['data-level' => 3]])->dropDownList(['']);
            echo $form->field($model, 'region', ['options' => ['data-level' => 2]])->dropDownList(['']);
            echo $form->field($model, 'company', ['options' => ['data-level' => 1]])->dropDownList(['']);
            ?>
            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <?= \yii\helpers\Html::submitButton('确认', ['class' => 'btn btn-primary']) ?>
                    <a href="index" class="btn btn-primary">取消</a>
                </div>
            </div>
            <?php \yii\bootstrap\ActiveForm::end(); ?>
        </div>
    </div>
</div>
<?php
$requestPrefix = 'http://' . str_replace('backend', 'api', $_SERVER['HTTP_HOST']) . '/service/';
$maxLevel = Yii::$app->user->identity->level;
$minLevel = Yii::$app->user->identity->user_name == 'root' ?  $maxLevel : $maxLevel - 1;
$district = Yii::$app->user->identity->relate_id;
$JS = <<<JS
var requestPrefix = '{$requestPrefix}';
var levels = [1, 2, 3, 4];
var levelList = {
    "1": 'company',
    "2": 'region',
    "3": 'city',
    "4": 'province'
};

var minLevel = {$minLevel};
var maxLevel = {$maxLevel};

$('.field-admin-province').hide();
$('.field-admin-city').hide();
$('.field-admin-region').hide();
$('.field-admin-company').hide();

ajaxShow(minLevel);

$.fn.ajaxShow = function() {
    if ($(this).data('level')) {
        if ($(this).css('display') == 'block')
            return;
        minLevel = $(this).data('level');
        ajaxShow(levels[levels.length - 1]);
    }
}

function ajaxShow(level) {
    if (level >= maxLevel)
        return;
    if (level < minLevel)
        return;
    var action = levelList[level];
    if (!action)
        return;
    if ($('.field-admin-' + action).css('display') == 'block') {
        level--;
        ajaxShow(level);
        return;
    }
    var pid = 0;
    if (level == maxLevel - 1) {
        pid = {$district};
    } else {
        var parent = levelList[level + 1];
        if (parent) {
            pid = $('#admin-' + parent).val();
        }
    }
    var url = requestPrefix + action + 's' + '?pid=' + pid;
    $.getJSON(url, function(json) {
        if (json.ret === 0) {
            var data = json.data;
            $('.field-admin-' + action).show();
            $('#admin-' + action).empty();
            var isFirst = 1;
            $.each(data, function(index, val) {
                var append;
                if (isFirst) {
                    append = '<option value = "' + index + '" selected = "selected">' + val + '</option>';
                    isFirst = 0;
                } else {
                    append = '<option value = "' + index + '">' + val + '</option>';
                }                    
                $('#admin-' + action).append(append);                    
            })
            level--;
            ajaxShow(level);
        }
    })  
}

$('.form-group select').change(function() {
    if ($(this).parents('.form-group').data('level')) {
        var level = parseInt($(this).parents('.form-group').data('level')) - 1;
        ajaxChange(level);
    }
})

function ajaxChange(level) {
    if (level >= maxLevel)
        return;
    if (level < minLevel)
        return;
    var action = levelList[level];
    if (!action)
        return;
    var pid = 0;
    if (level == maxLevel - 1) {
        pid = {$district};
    } else {
        var parent = levelList[level + 1];
        if (parent) {
            pid = $('#admin-' + parent).val();
        }
    }
    var url = requestPrefix + action + 's' + '?pid=' + pid;
    $.getJSON(url, function(json) {
        if (json.ret === 0) {
            var data = json.data;
            $('.field-admin-' + action).show();
            $('#admin-' + action).empty();
            var isFirst = 1;
            $.each(data, function(index, val) {
                var append;
                if (isFirst) {
                    append = '<option value = "' + index + '" selected = "selected">' + val + '</option>';
                    isFirst = 0;
                } else {
                    append = '<option value = "' + index + '">' + val + '</option>';
                }                    
                $('#admin-' + action).append(append);                    
            })
            level--;
            ajaxChange(level);
        }
    })    
}

$('#admin-level').change(function(){
    var level = $(this).val();
    $.each(levels, function(index, val) {
        if (val < level) {
            $('.field-admin-' + levelList[val]).hide();
        }        
    })
    $('.field-admin-' + levelList[level]).ajaxShow();
})

JS;
$this->registerJs($JS);

